alphabet = "abcdefghijklmnopqrstuvwxyz"

import math
import random

class LetterMap:

	def __init__(self, token):
		self.token = token
		self.totalSize = 0
		self.letterMap = {}

	def addLetter(self, toLetter):
		if not toLetter in alphabet:
			return

		self.totalSize += 1
		value = self.letterMap.get(toLetter, 0)
		self.letterMap[toLetter] = value + 1

	def getLetter(self, random):
		i = random.randint(0, self.totalSize)
		pos = 0
		for letter, number in self.letterMap.items():
			if i > pos and i <= pos + number:
				return letter
			pos += number
		return None

class NameGenerator:
	def __init__(self, seed, sl):
		self.sl = sl
		self.nameMap = {}
		self.numNames = 0
		self.numCharacters = 0
		self.random = random.Random(seed)
		self.wordSizes = []
		
		self.made_names = []
		

	def inputFile(self, fileName):
		f = open(fileName)
		text = f.read()
		f.close()
		words = text.split(" ")
		self.words = words
		self.input(words)

	def input(self, inputNames):
		for name in inputNames:
			for i in range(0, len(name)-self.sl):
				fromToken = name[i:i+self.sl]
				nextChar = name[i+self.sl]
				if not fromToken[0] in alphabet:
					continue
				lettermap = self.nameMap.get(fromToken, LetterMap(fromToken))
				lettermap.addLetter(nextChar)
				self.nameMap[fromToken] = lettermap

			self.numNames += 1
			self.numCharacters += len(name)
			self.wordSizes.append(len(name))
		self.calcStandardDeviation()

	def calcStandardDeviation(self):
		self.mean = self.numCharacters / self.numNames
		variance = 0
		for size in self.wordSizes:
			variance += (self.mean-size)**2
		self.standardDeviation = math.sqrt(variance/float(self.numNames))

	def makeName(self):
		size = self.random.gauss(self.mean, self.standardDeviation)
		output = self.random.choice(list(self.nameMap.keys()))
		for i in range(0,int(size)):
			lastChar = output[-2:]
			letterMap = self.nameMap.get(lastChar, None)
			if letterMap:
				newChar = letterMap.getLetter(self.random)
				if newChar:
					output += newChar
					
					
		return str( output[:1].lower() + output[1:] )
		
	def getName(self):
		if len(self.made_names) == 0:
			for i in range(100):
				self.made_names.append( self.makeName() )
		else:
			return self.made_names.pop(0)